﻿using System;
using System.ComponentModel.Composition;
using System.IO;
using System.Windows.Forms;
using Moonlite.Composition;
using Moonlite.ExtensionApi.ControlInterfaces;
using Moonlite.ExtensionApi.GlobalInterfaces;
using Moonlite.Platform.General;

namespace Moonlite.Modules.Editors.DataTools
{
    [Export("Moonlite.Composition.WindowExtensionContract", typeof(UserControl))]
    [WindowMetadata(Dock = DockStyle.Bottom, DockId = 0, Flags = WindowFlags.None, Hidden = true, Name = "EventRegisterer")]
    public class EventRegisterer : UserControl
    {
        private void OnProjectOpened(object sender, ProjectEventArgs e)
        {
            IProjectManager projectManager = ((IProjectManager)((object[])(Global.Instance.GetService(typeof(IProjectManager))))[0]);
            DataCustomFolderNode dataNode = new DataCustomFolderNode(projectManager);
            dataNode.Text = "Data";
            projectManager.AddCustomNode(dataNode);
            dataNode.UpdateChildren();
            dataNode.UpdateImage();
        }

        public EventRegisterer()
        {
            Global.Instance.ProjectOpened += OnProjectOpened;
            IProjectManager projectManager = ((IProjectManager)((object[])(Global.Instance.GetService(typeof(IProjectManager))))[0]);
            projectManager.AddFileExtensionAssociation(".xml", (IProjectFile file, TreeNode node) =>
            {
                if (node is DataCustomNode)
                {
                    DataViewer dataViewer = new DataViewer();
                    dataViewer.Open((string)node.Tag);
                    Global.Instance.OpenDocument(node.Text, dataViewer);
                }
            });
        }
    }
}
